package ar.uade.edu.fain.ad.client.web.servlet.salon.aperturaMesa;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ar.uade.edu.fain.ad.client.web.servlet.salon.SalonServlet;
import ar.uade.edu.fain.ad.common.transfer.MesaVO;
import ar.uade.edu.fain.ad.common.transfer.SalonVO;
import ar.uade.edu.fain.ad.common.transfer.SectorVO;

public class SeleccionSectorServlet extends SalonServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SeleccionSectorServlet() {
		super();
	}
	
	@Override
	protected String getViewBasePath() {
		return super.getViewBasePath() + "aperturaMesa/";
	}
	/**
	 * Entrada, busca los sectores. 
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		final HttpSession session = request.getSession();
		final Long salonId = (Long) session.getAttribute("salonId");
		final SalonVO salonVO = new SalonVO();
		salonVO.setId(salonId);
		final List<SectorVO> sectores = 
				getSalonServiceDelegate().buscarSectoresEn(salonVO);
		request.setAttribute("sectores", sectores);
		forward(getViewBasePath() + "seleccionarSector.jsp", request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		final String sectorId = request.getParameter("sectorId");
		final String cantidadComensales = request.getParameter("cantidadComensales");
		final SectorVO sectorVO = new SectorVO();
		sectorVO.setId(Long.valueOf(sectorId));
		final Set<MesaVO> mesas = 
				getSalonServiceDelegate().buscarMesasDisponiblesEn(
						sectorVO, Integer.valueOf(cantidadComensales));
		final ArrayList<MesaVO> mesasList = new ArrayList<MesaVO>(mesas);
		Collections.sort(mesasList, new Comparator<MesaVO>() {
			@Override
			public int compare(MesaVO o1, MesaVO o2) {
				return o1.getId().compareTo(o2.getId());
			}
		});
		request.setAttribute("mesas", mesasList);
		forward(getViewBasePath() + "seleccionarMesas.jsp", request, response);
	}
	
}
